<?php

namespace app\admin\controller;
use app\BaseController;
use think\facade\Db;

class Activity extends BaseController {

  public function info(){
    $id = request()->param('id');
    if (!$id) {
      returnJson(-2, '参数错误');
    }
    $data = Db::table('activity')->where('id', $id)->find();
    if (!$data) {
      returnJson(-2, '查询失败！');
    }
    $data['goods_ids'] = explode(',', $data['goods_ids']);
    foreach ($data['goods_ids'] as $key => &$value) {
      $value = Intval($value);
    }

    $banner = Db::table('activity_banner')->order('id', 'asc')->where('a_id', $data['id'])->select();
    
    $data = [
      'info' => $data,
      'banner' => $banner,
    ];

    returnJson(0, '成功！', $data);
  }

  public function list() {
    $request = request()->param();

    //分页查询
    $page = pageQuery($request);

    $goodsList = Db::table('activity')->order('id', 'desc')->page($page['page'], $page['count'])->select();
    if (!$goodsList) {
      returnJson(-2, '查询失败！');
    }
    $goodsCount = Db::table('activity')->count(); //总条数
    
    $data = [
      'count' => $page['count'],
      'total' => $goodsCount,
      'rows' => $goodsList,
    ];
    returnJson(0, '成功！', $data);
  }

  public function add() {
    $request = request()->param();

    $goods_ids = '';
    if (isset($request['goods_ids']) && is_array($request['goods_ids'])){
      $goods_ids = implode(',', $request['goods_ids']);
    }
    
    $data = [
      'title' => request()->param('title'),
      'start_time' => request()->param('start_time'),
      'end_time' => request()->param('end_time'),
      'goods_ids' => $goods_ids,
      'token' => md5(time()),
      'tmp' => request()->param('tmp'),
    ];
    
    $a_id = Db::table('activity')->insertGetId($data);
    if ($a_id) {
      // 插入轮播图
      $banner =  request()->param('banner');
      if ($banner && is_array($banner)) {
        $bannerData = [];
        foreach ($banner as $key => $value) {
          $bannerData[] = [
            'path' => $value['response']['data'],
            'a_id' => $a_id
          ];
        }
        if (!empty($bannerData)){
          Db::table('activity_banner')->insertAll($bannerData);
        }
      }

      returnJson(0, '添加成功！');
    }
    returnJson(-2, '添加失败！');

     
    
  }

  public function update (){
    $request = request()->param();
    if (!isset($request['id'])) {
      returnJson(-2, '参数错误');
    }

    $goods_ids = '';
    if (isset($request['goods_ids']) && is_array($request['goods_ids'])){
      $goods_ids = implode(',', $request['goods_ids']);
    }

    $a_id = $request['id'];
    $data = [
      'title' => request()->param('title'),
      'start_time' => request()->param('start_time'),
      'end_time' => request()->param('end_time'),
      'goods_ids' => $goods_ids,
      'tmp' => request()->param('tmp'),
    ];
    
    $update = Db::table('activity')->where('id', $a_id)->update($data);
    if ($update >= 0) {

      // 插入轮播图
      Db::table('activity_banner')->where('a_id', $a_id)->delete();
      $banner =  request()->param('banner');
      if ($banner && is_array($banner)) {
        $bannerData = [];
        foreach ($banner as $key => $value) {
          if (isset($value['response'])){
            $bannerData[] = [
              'path' => $value['response']['data'],
              'a_id' => $a_id
            ];
          }else{
            $bannerData[] = [
              'path' => $value['url'],
              'a_id' => $a_id
            ];
          }
        }
        if (!empty($bannerData)){
          Db::table('activity_banner')->insertAll($bannerData);
        }
      }

      returnJson(0, '修改成功！');
    }
    returnJson(-2, '修改失败！');
  }

  public function delete () {
    $ids = request()->param('ids');
    $idsArr = explode(',', $ids);
    
    if (is_array($idsArr)){
      $del = Db::table('activity')->delete($idsArr);
      returnJson(0, '删除成功！');
    }
    returnJson(-2, '删除失败！');
  }
  
}